package org.skydiveapphd;

import org.skydiveapphd.ui.FragmentHelper;
import org.skydiveapphd.interfaces.NotesNotifier;
import org.skydiveapphd.interfaces.RigComponentNotifier;
import org.skydiveapplibrary.R;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class RigComponentEditActivity extends Fragment implements NotesNotifier
{
	// data
	private String notes = "";
	private String name;
	private String serialNumber;
	private Boolean isEdit = false;

	public static RigComponentEditActivity newInstance(Bundle args) 
	{
		RigComponentEditActivity f = new RigComponentEditActivity();
		// Supply index input as an argument.
		f.setArguments(args);
		return f;
	}
	
	// listeners
	private OnClickListener saveListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			save();
		}
	};
	private OnClickListener cancelListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			getFragmentManager().popBackStack();
		}
	};
	private OnClickListener notesEditListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			commitChanges();
			
			Bundle args = new Bundle();
			args.putString(Constants.EXTRA_FRAG_TAG, RigComponentEditActivity.this.getTag());
			args.putString(Constants.EXTRA_NOTES, notes);

			FragmentHelper.getInstance().openFragment(NotesEditActivity.newInstance(args), android.R.id.tabhost, 
					Constants.TAG_NOTES_EDIT, getFragmentManager());
		}
	};
	private OnClickListener deleteListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			delete();
		}
	};
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		loadData();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    		Bundle savedInstanceState)
	{	
	    // Inflate the layout for this fragment
	    return inflater.inflate(R.layout.rig_component_edit_view, container, false);
	}
	
	@Override
    public void onResume()
    {
    	super.onResume();
    	
		// init ui
		initializeUI();
    }
	   
	private void loadData()
	{
		// get data
		isEdit = (getArguments().getBoolean(Constants.EXTRA_RIG_COMPONENT_IS_EDIT, false));
		name = (getArguments().containsKey(Constants.EXTRA_RIG_COMPONENT_NAME)) ?
				getArguments().getString(Constants.EXTRA_RIG_COMPONENT_NAME) : "";
		serialNumber = (getArguments().containsKey(Constants.EXTRA_RIG_COMPONENT_SERIAL_NUMBER)) ?
				getArguments().getString(Constants.EXTRA_RIG_COMPONENT_SERIAL_NUMBER) : "";
		notes = (getArguments().containsKey(Constants.EXTRA_RIG_COMPONENT_NOTES)) ?
				getArguments().getString(Constants.EXTRA_RIG_COMPONENT_NOTES) : "";
	}
	
	private void initializeUI()
	{
		// set header text
		TextView headerTextView = (TextView) getActivity().findViewById(R.id.EditHeaderTextView);
		headerTextView.setText(R.string.rig_component_header);
		
		// set name
		EditText nField = (EditText) getActivity().findViewById(R.id.RigComponentNameField);
		nField.setText(name);
		
		// set serial number
		EditText snField = (EditText) getActivity().findViewById(R.id.RigComponentSerialNumberField);
		snField.setText(serialNumber);
		
		// set notes
		TextView notesField = (TextView) getActivity().findViewById(R.id.RigComponentNotesField);
		notesField.setOnClickListener(notesEditListener);
		notesField.setText(notes);
		
		// add button listeners
		ImageButton saveButton = (ImageButton) getActivity().findViewById(R.id.RigComponentSaveButton);
		saveButton.setOnClickListener(saveListener);

		ImageButton cancelButton = (ImageButton)getActivity().findViewById(R.id.RigComponentCancelButton);
		cancelButton.setOnClickListener(cancelListener);
		
		ImageButton deleteButton = (ImageButton) getActivity().findViewById(R.id.RigComponentDeleteButton);
		deleteButton.setOnClickListener(deleteListener);
		deleteButton.setVisibility(isEdit ? View.VISIBLE : View.GONE);
	}
	
	public void updateNotes(String notes)
	{
		this.notes = notes;
	}

	private void commitChanges()
	{
		// get name from field
		EditText nField = (EditText) getActivity().findViewById(R.id.RigComponentNameField);
		name = nField.getText().toString();
		
		// get serial number from field
		EditText snField = (EditText) getActivity().findViewById(R.id.RigComponentSerialNumberField);
		serialNumber = snField.getText().toString();
	}
	
	private void save()
	{
		commitChanges();
		
		// set result
		RigComponentNotifier f = (RigComponentNotifier) getFragmentManager().findFragmentByTag(getArguments().getString(Constants.EXTRA_FRAG_TAG));
		f.updateComponent(name, serialNumber, notes);
		
		// finish
		getFragmentManager().popBackStack();
	}
	
	private void delete()
	{
		RigComponentNotifier f = (RigComponentNotifier) getFragmentManager().findFragmentByTag(getArguments().getString(Constants.EXTRA_FRAG_TAG));
		f.deleteComponent();
		
		// finish
		getFragmentManager().popBackStack();
	}
}
